package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.eng.EnglishEntity;
import com.wq.wrongquestion.service.eng.EnglishService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/eng")
public class EnglishController {
    @Autowired
    public EnglishService englishService;
    @Autowired
    public FileUploadUtils fileUploadUtils;

    final String IMG_PAHT_YT_ENG = "images/eng/yt/";
    final String IMG_PAHT_DA_ENG = "images/eng/da/";            
    /************************************************English Start************************************************/
    @RequestMapping("/englishInit") 
    public String englishInit(Model model){
        //章节
        List<String> allZj=englishService.getEnglishZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new EnglishEntity());
        
        return "eng/englishSearch";
    }


    @PostMapping("/englishIns")
    public String insEnglish(EnglishEntity englishEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_ENG);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_ENG);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                englishEntity.setImgytnm(fileName);
                englishEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                englishEntity.setImgdanm(fileName2);
                englishEntity.setImgdapath(filePath2);
            }
            // 插入数据
            englishEntity.setFxcs(1);
            englishEntity.setXueke(englishEntity.getHidXueke());

            englishService.insEnglish(englishEntity);

            // return "redirect:/eng/englishInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/eng/addEnglish";
        }
        return "redirect:/eng/englishInit";
    }
    @RequestMapping("/addEnglish")
    public String addEnglish(Model model){
        List<String> allZj=englishService.getEnglishZj();
        
        model.addAttribute("allZj", allZj);
        return "eng/addEnglish";
    }

    @RequestMapping("/searchEnglish")
    public String searchEnglish(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, EnglishEntity englishEntity) {

        PageInfo<EnglishEntity> english = englishService.getEnglish(pageNum, pageSize, englishEntity);
        List<String> allZj=englishService.getEnglishZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("english", english);
        model.addAttribute("condEntity", englishEntity);

        return "eng/englishSearch";
    }

    @RequestMapping("/searchEnglishByID")
    public String searchEnglishByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<EnglishEntity> englishByID = englishService.getEnglishByID(id);
        List<String> allZj=englishService.getEnglishZj();
        if(!englishByID.isEmpty()){
            model.addAttribute("english", englishByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "eng/englishSearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "eng/modifyEnglish";
    }

    @PostMapping("/englishUpd")
    public String englishUpd(Model model, EnglishEntity englishEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_ENG);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        englishEntity.setImgytnm(fileName);
                        englishEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_ENG);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        englishEntity.setImgdanm(fileName2);
                        englishEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            englishEntity.setFxcs(1);
            englishEntity.setXueke(englishEntity.getHidXueke());

            Integer updRes=englishService.updEnglish(englishEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<EnglishEntity> englishByID = englishService.getEnglishByID(englishEntity.getId());           
            if(englishByID.isEmpty()){
               englishByID = new ArrayList<EnglishEntity>();
            }
            model.addAttribute("english", englishByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "eng/modifyEnglish";   
    }

    @PostMapping("/englishDel")
    public String englishDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        englishService.delEnglish(id);
        
        return "redirect:/eng/englishInit";
    }    
    /************************************************English End************************************************/
}
